package com.mediacloud.app.newsmodule.fragment.recommend;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.mediacloud.app.model.component.ComponentItem;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.ShowSwitch;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.activity.RelatedListActivity;
import com.mediacloud.app.newsmodule.adaptor.component.XAbsComponentHolder;
import com.mediacloud.app.newsmodule.drama.PopularRecommendBean;
import com.mediacloud.app.newsmodule.fragment.baoliao.api.SpiderCmsApi;
import com.utils.ClickUtils;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author
 * @date 2022/12/27.
 * email：guangmingfei@chinamcloud.com
 * description：
 */
public abstract class BaseComponentPopularRecommendView extends FrameLayout {
    public ShowSwitch showSwitch;
    protected View mRootView = null;
    public int type = 1;

    public String componentId;
    public String article_id;
    public String vid;

    protected XAbsComponentHolder header;

    public BaseComponentPopularRecommendView(Context context) {
        this(context, null);
    }

    public BaseComponentPopularRecommendView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    protected String tipoff_catalogid;

    public BaseComponentPopularRecommendView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initOtherViews();
        this.addView(mRootView);
    }

    /**
     * 更多+标题
     *
     * @param context
     */
    protected void initView(Context context) {
        this.setVisibility(GONE);
        LayoutInflater inflater = LayoutInflater.from(context);
        mRootView = inflater.inflate(setLayoutRes(), null);
        mRootView.setVisibility(View.GONE);

        mRootView.findViewById(R.id.line_title).setVisibility(View.VISIBLE);
        if (mRootView.findViewById(R.id.root_title) != null) {
            mRootView.findViewById(R.id.root_title).setVisibility(View.VISIBLE);
        }

        TextView title = mRootView.findViewById(R.id.titleNameText);
        title.setText("精彩推荐");

        header = new XAbsComponentHolder(mRootView) {
            @Override
            public boolean isDetail() {
                return true;
            }

            @Override
            public void onClick(View v) {
                ClickUtils.delayClickable(v);
                onMoreClick(v);
                Intent intent = new Intent();
                intent.setClass(getContext(), RelatedListActivity.class);
                if (!TextUtils.isEmpty(tipoff_catalogid)) {
                    intent.putExtra("tipoff_catalogid", tipoff_catalogid);
                }
                intent.putExtra("componentId", componentId);
                intent.putExtra("article_id", article_id);
                intent.putExtra("type", type);
                getContext().startActivity(intent);
            }
        };
    }

    public abstract int setLayoutRes();

    public abstract void initOtherViews();

    public abstract void onDataSuccess(ArrayList<ArticleItem> data);

    public abstract void onDataFault(Object data);

    protected ComponentItem componentItem;
    protected ArticleItem articleItem;

    public void setData(String authorIds) {
        getPopularRecommendList(authorIds);
    }

    /**
     * 获取关注推荐列表
     */
    public void getPopularRecommendList(String authorIds) {
        SpiderCmsApi.getSpiderCmsApi(getContext()).getPopularReCommend(authorIds, getContext().getString(R.string.tenantid)).enqueue(new Callback<PopularRecommendBean>() {
            @Override
            public void onResponse(Call<PopularRecommendBean> call, Response<PopularRecommendBean> response) {
                if (response.body().data != null && response.body().data.pageRecords != null)
                    onDataSuccess(response.body().data.pageRecords);
            }

            @Override
            public void onFailure(Call<PopularRecommendBean> call, Throwable t) {
                Log.i("------getRelateNewsList-----", t.getMessage());
            }
        });
    }
}
